﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Duoease.ContainerModel.Entity
{
    public class LiquidNitrogenJar : BaseModel
    {
         #region 构造函数
        public LiquidNitrogenJar()
        {
            this.Title = "液氮罐";
            this.ElementType = "fridge";
            this.TagName = "LiquidNitrogenJar";
        }
        public LiquidNitrogenJar(string tile)
        {
            this.Title = tile;
            this.ElementType = "fridge";
            this.TagName = "LiquidNitrogenJar";
        }
        public LiquidNitrogenJar(int row, int column, string tile)
        {
            Row = row;
            Column = column;
            this.Title = tile;
            this.ElementType = "fridge";
            this.TagName = "LiquidNitrogenJar";
        }
        public LiquidNitrogenJar(int row, int column, Color surfaceColor, int alpha, string tile)
        {
            Row = row;
            Column = column;
            SurfaceColor = surfaceColor;
            Alpha = alpha;
            this.Title = tile;
            this.ElementType = "fridge";
            this.TagName = "LiquidNitrogenJar";
        }
        public LiquidNitrogenJar(int row, int column, Color surfaceColor, int alpha, Color lineColor, int lineWidth, string tile)
        {
            Row = row;
            Column = column;
            SurfaceColor = surfaceColor;
            Alpha = alpha;
            LineColor = lineColor;
            LineWidth = lineWidth;
            this.Title = tile;
            this.ElementType = "fridge";
            this.TagName = "LiquidNitrogenJar";
        }
        #endregion

         #region 属性
        private Point _pc1 = new Point();
        public Point pc1
        {
            get { return _pc1; }
            set { _pc1 = value; }
        }

        private Point _pc2 = new Point();
        public Point pc2
        {
            get { return _pc2; }
            set { _pc2 = value; }
        }

        private Point _pc3 = new Point();
        public Point pc3
        {
            get { return _pc3; }
            set { _pc3 = value; }
        }

        private Point _pc4 = new Point();
        public Point pc4
        {
            get { return _pc4; }
            set { _pc4 = value; }
        }

        private Point _pc5 = new Point();
        public Point pc5
        {
            get { return _pc5; }
            set { _pc5 = value; }
        }

        private Point _pc6 = new Point();
        public Point pc6
        {
            get { return _pc6; }
            set { _pc6 = value; }
        }

        private int _radius = 22;//小圆半径
        public int radius
        {
            get { return _radius; }
        }

        private int _IsC = 0;
        public int IsC
        {
            get { return _IsC; }
            set { _IsC = value; }
        }
        #endregion
         /**
         * 功能：鼠标点击事件
         * 
         * */
        public override void OnMouseClickHandler(object sender, MouseEventArgs e)
        {
            Point p = new Point(e.X, e.Y);
            int x = 0;
            int y = 0;
            g = this.panel.CreateGraphics();

            this.g = g;
            Draw();
            Render(g, p);

            this.View.MouseRow = 0;
            this.View.MouseColumn = IsC;
        }

        /**
     * 功能：图形绘制
     * 
     * */
        public override void Draw()
        {

            g.Clear(BackGround);
            //this.Location.X = (this.CanvasWidth - this.ContainerWidth)/2 > ContainerWidth / (Column + 2) ? ContainerWidth / (Column + 2) : (this.CanvasWidth - this.ContainerWidth)/2;
            //this.Location.Y = (this.CanvasHeight - this.ContainerHeight)/2 < ContainerHeight / (Row + 2) ? ContainerHeight / (Row + 2) : (this.CanvasHeight - this.ContainerHeight)/2;

            Point p1 = new Point(ContainerWidth / 4, ContainerHeight * 2 / 10);
            Point p2 = new Point(ContainerWidth * 3 / 4, p1.Y);
            Point p5 = new Point(ContainerWidth / 6, ContainerHeight * 7 / 10);
            Point p6 = new Point(ContainerWidth * 5 / 6, p5.Y);
            Point p3 = new Point(p5.X, ContainerHeight * 3 / 10);
            Point p4 = new Point(p6.X, p3.Y);
            Point[] pts = new Point[4];
            pts[0] = p1;
            pts[1] = p2;
            pts[2] = p4;
            pts[3] = p3;
            g.FillPolygon(new SolidBrush(Color.Gray), pts);
            g.FillEllipse(new SolidBrush(Color.White), ContainerWidth / 4, ContainerHeight / 10, ContainerWidth / 2, ContainerHeight / 5);
            g.DrawEllipse(new Pen(new SolidBrush(Color.Black), 2), ContainerWidth / 4, ContainerHeight / 10, ContainerWidth / 2, ContainerHeight / 5);
            g.FillEllipse(new SolidBrush(Color.Gray), ContainerWidth / 6, ContainerHeight * 6 / 10, ContainerWidth * 2 / 3, ContainerHeight / 5);
            g.DrawEllipse(new Pen(new SolidBrush(Color.Black), 2), ContainerWidth / 6, ContainerHeight * 6 / 10, ContainerWidth * 2 / 3, ContainerHeight / 5);
            g.DrawLine(new Pen(new SolidBrush(Color.Black), 2), p1, p3);
            g.DrawLine(new Pen(new SolidBrush(Color.Black), 2), p2, p4);
            g.FillRectangle(new SolidBrush(Color.Gray), p3.X, p3.Y, p6.X - p5.X, p5.Y - p3.Y);
            g.DrawLine(new Pen(new SolidBrush(Color.Black), 2), p3, p5);
            g.DrawLine(new Pen(new SolidBrush(Color.Black), 2), p4, p6);
            Pen pen = new Pen(new SolidBrush(Color.Black), 2);
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            g.DrawEllipse(pen, ContainerWidth / 6, ContainerHeight * 6 / 10, ContainerWidth * 2 / 3, ContainerHeight / 5);

            //int width = ContainerWidth / 2;
            //int height = ContainerHeight / 5;        
            //int tempX = 15;            
            //int intX = Convert.ToInt32(radius * Math.Cos(45*Math.PI/180));
            //Point center = new Point(p1.X + width / 2, p1.Y);
            //pc1 = new Point(center.X-intX, center.Y - height / 4-intX-tempX);
            //pc2 = new Point(center.X + Convert.ToInt32((Math.Cos(30 * Math.PI / 180) * height / 4)-intX)+tempX, center.Y - Convert.ToInt32(Math.Sin(30 * Math.PI / 180) * height / 4)-intX-tempX);
            //pc3 = new Point(pc2.X, Convert.ToInt32(center.Y + Math.Sin(30 * Math.PI / 180) * height / 4)-intX);
            //pc4 = new Point(pc1.X, center.Y + height / 4 - intX);
            //pc5 = new Point(center.X - Convert.ToInt32(Math.Cos(30 * Math.PI / 180) * height / 4)-intX-tempX, pc3.Y);
            //pc6 = new Point(pc5.X, pc2.Y);

            GetPoints();
            pen = new Pen(new SolidBrush(LineColor), LineWidth);
            g.DrawEllipse(pen, pc1.X, pc1.Y, radius * 2, radius * 2);
            g.DrawEllipse(pen, pc2.X, pc2.Y, radius * 2, radius * 2);
            g.DrawEllipse(pen, pc3.X, pc3.Y, radius * 2, radius * 2);
            g.DrawEllipse(pen, pc4.X, pc4.Y, radius * 2, radius * 2);
            g.DrawEllipse(pen, pc5.X, pc5.Y, radius * 2, radius * 2);
            g.DrawEllipse(pen, pc6.X, pc6.Y, radius * 2, radius * 2);

            SolidBrush brush = new SolidBrush(Color.FromArgb(Alpha, SurfaceColor));
            g.FillEllipse(brush, pc1.X, pc1.Y, radius * 2, radius * 2);
            g.FillEllipse(brush, pc2.X, pc2.Y, radius * 2, radius * 2);
            g.FillEllipse(brush, pc3.X, pc3.Y, radius * 2, radius * 2);
            g.FillEllipse(brush, pc4.X, pc4.Y, radius * 2, radius * 2);
            g.FillEllipse(brush, pc5.X, pc5.Y, radius * 2, radius * 2);
            g.FillEllipse(brush, pc6.X, pc6.Y, radius * 2, radius * 2);
        }

        public void GetPoints()
        {
            if (ContainerWidth == 0 && ContainerHeight == 0)
            {
                return;
            }
            Point p1 = new Point(ContainerWidth / 4, ContainerHeight * 2 / 10);
            int width = ContainerWidth / 2;
            int height = ContainerHeight / 5;
            int tempX = 15;

            int intX = Convert.ToInt32(radius * Math.Cos(45 * Math.PI / 180));
            Point center = new Point(p1.X + width / 2, p1.Y);
            pc1 = new Point(center.X - intX, center.Y - height / 4 - intX - tempX);
            pc2 = new Point(center.X + Convert.ToInt32((Math.Cos(30 * Math.PI / 180) * height / 4) - intX) + tempX, center.Y - Convert.ToInt32(Math.Sin(30 * Math.PI / 180) * height / 4) - intX - tempX);
            pc3 = new Point(pc2.X, Convert.ToInt32(center.Y + Math.Sin(30 * Math.PI / 180) * height / 4) - intX);
            pc4 = new Point(pc1.X, center.Y + height / 4 - intX);
            pc5 = new Point(center.X - Convert.ToInt32(Math.Cos(30 * Math.PI / 180) * height / 4) - intX - tempX, pc3.Y);
            pc6 = new Point(pc5.X, pc2.Y);
        }

        public void Render(Graphics g, Point mouse)
        {
            if (mouse.X > pc1.X && mouse.Y > pc1.Y && mouse.X < pc1.X + radius * 2 && mouse.Y < pc1.Y + radius * 2)
            {
                IsC = 1;
            }
            if (mouse.X > pc2.X && mouse.Y > pc2.Y && mouse.X < pc2.X + radius * 2 && mouse.Y < pc2.Y + radius * 2)
            {
                IsC = 2;
            }
            if (mouse.X > pc3.X && mouse.Y > pc3.Y && mouse.X < pc3.X + radius * 2 && mouse.Y < pc3.Y + radius * 2)
            {
                IsC = 3;
            }
            if (mouse.X > pc4.X && mouse.Y > pc4.Y && mouse.X < pc4.X + radius * 2 && mouse.Y < pc4.Y + radius * 2)
            {
                IsC = 4;
            }
            if (mouse.X > pc5.X && mouse.Y > pc5.Y && mouse.X < pc5.X + radius * 2 && mouse.Y < pc5.Y + radius * 2)
            {
                IsC = 5;
            }
            if (mouse.X > pc6.X && mouse.Y > pc6.Y && mouse.X < pc6.X + radius * 2 && mouse.Y < pc6.Y + radius * 2)
            {
                IsC = 6;
            }

            SolidBrush brush1 = new SolidBrush(Color.Blue);
            SolidBrush brush2 = new SolidBrush(Color.Yellow);
            switch (IsC)
            {
                case 1:
                    g.FillEllipse(brush1, pc1.X, pc1.Y, radius * 2, radius * 2);
                    break;
                case 2:
                    g.FillEllipse(brush1, pc2.X, pc2.Y, radius * 2, radius * 2);
                    break;
                case 3:
                    g.FillEllipse(brush1, pc3.X, pc3.Y, radius * 2, radius * 2);
                    break;
                case 4:
                    g.FillEllipse(brush1, pc4.X, pc4.Y, radius * 2, radius * 2);
                    break;
                case 5:
                    g.FillEllipse(brush1, pc5.X, pc5.Y, radius * 2, radius * 2);
                    break;
                case 6:
                    g.FillEllipse(brush1, pc6.X, pc6.Y, radius * 2, radius * 2);
                    break;
            }
        }

        public override void AffixToLabel(Label oldLabel, int width, int height)
        {
            Label newLabel = new Label();
            this.panel.Controls.Clear();
            this.panel.Controls.Add(oldLabel);

            this.ContainerWidth = width;
            this.ContainerHeight = height;
            this.GetPoints();
            Point[] ps = new Point[6];
            ps[0] = new Point(this.pc1.X + 14, this.pc1.Y + 14);
            ps[1] = new Point(this.pc2.X + 14, this.pc2.Y + 14);
            ps[2] = new Point(this.pc3.X + 14, this.pc3.Y + 14);
            ps[3] = new Point(this.pc4.X + 14, this.pc4.Y + 14);
            ps[4] = new Point(this.pc5.X + 14, this.pc5.Y + 14);
            ps[5] = new Point(this.pc6.X + 14, this.pc6.Y + 14);

            for (int i = 0; i < 6; i++)
            {
                newLabel = new Label();
                newLabel.Width = 18;
                newLabel.Text = (i + 1).ToString();
                newLabel.Font = new Font(newLabel.Font.FontFamily, 15, FontStyle.Bold);

                newLabel.MouseDown += new MouseEventHandler(this.View.InewLabel_MouseDown);
                newLabel.ForeColor = Color.Black;
                newLabel.BackColor = Color.Transparent;
                newLabel.Location = ps[i];
                // newLabel.Parent = this.splitContainer1.Panel1;
                this.panel.Controls.Add(newLabel);

            }
        }
    }
}
